package com.zt.rongda.stock.service;

import java.text.SimpleDateFormat;
import java.util.List;

import com.zt.rongda.stock.inter.StockAlertMapper;
import com.zt.rongda.stock.model.StockAlert;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StockAlertService extends BaseService {

    @Autowired
    private StockAlertMapper stockAlertMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select a.*,b.ALERT_NUM from STOCK a, STOCK_TYPE b where a.TYPE_ID = b.TYPE_ID ");

        sqlBuilder.addWhereLike2("STOCK_NAME", reqBean.getInfo("stockName"));

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param stockId
    * @return
    */
    public StockAlert getById(String stockId) {
        return stockAlertMapper.selectByPrimaryKey(stockId);
    }

    /**
    * 新增
    *
    * @param stockAlert
    */
    public void add(StockAlert stockAlert) {
        stockAlert.setStockId(stringId());// 手动设置Id

        stockAlertMapper.insert(stockAlert);
    }

    /**
    * 修改
    *
    * @param stockAlert
    */
    public void update(StockAlert stockAlert) {
        String stockId = stockAlert.getStockId();
        if (StringUtil.isEmpty(stockId)) {
            throw new BusinessException("获取主键失败");
        }

        stockAlertMapper.updateByPrimaryKeySelective(stockAlert);
    }

    /**
    * 删除
    *
    * @param stockId
    */
    public void delete(String stockId) {
        if (StringUtil.isEmpty(stockId)) {
            throw new BusinessException("获取主键失败");
        }
        stockAlertMapper.deleteByPrimaryKey(stockId);
    }

}